define(
    function (require) {
        require('tpl!./list.tpl.html');

        var util = require('common/util');

        var globalData = require('common/GlobalData').getInstance();

        var showid = 1;
        var page =1;

        var fields = [
            {
                title: '编号',
                field: 'id' ,
                sortable: true,
                resizable: true,
                width: 20,
                content: function (item) {
                    return showid++;
                }
            },
            {
                title: '设备名称',
                field: 'name' ,
                resizable : true,
                width: 120,
                content: function (item) {
                    // return '<span class="list-link" data-command-args="' + item.id + '" data-command="modify">' + item.name + '</a>';
                    return '<a class="list-link" href="/echarts.html?sbno='+item.sbNo+'"   target="view_window">' + item.name + '</a>';
                }
            },
            {
                title: '设备编号',
                field: 'sbNo' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.sbNo;
                }
            },
            {
                title: '设备规格',
                field: 'sbFormat' ,
                resizable : true,
                width: 100,
                content: function (item) {
                    return item.sbFormat;
                }
            },
            {
                title: '检测数量',
                field: 'orderCount' ,
                width: 80,
                content: function (item) {
                    return item.orderCount;
                }

            },
            {
                title: '存放名称',
                field: 'addressname' ,
                width: 100,
                content: function (item) {
                    return item.addressname;
                }
            },
            {
                title: '当前位置',
                field: 'nowaddress' ,
                width: 100,
                content: function (item) {
                    return buld(item.nowaddress);
                }
            },
            {
                title: '操作',
                field: 'operate' ,
                width: 100,
                content: function (item) {
                    var operates = [
                        '<span class="list-link" data-command-args="'
                        + item.id
                        + '" data-command="modify">查看</a></span>',
                        '<span class="list-link" data-command-args="'
                        + item.id
                        + '" data-command="delete">删除</a></span>'
                    ];
                    return operates.join('');
                }
            }
        ];
        //判断是否投放
        function buld(obj) {
            if(obj !== null){
                return obj;
            }else{
                return "暂未投放";
            }
        }
        //检索
        function search() {
            var equipmentShopName = this.get('equipmentShopName');
            this.fire('search', { equipmentShopName: equipmentShopName.getValue()});
        }
        //分页
        function flip(e) {
            var page = e.target.getAttribute('data-page');
            if (/(\+|\-)/.test(page)) {
                page = parseInt(page) + (RegExp.$1 === '+' ? 1 : -1);
            }
            this.fire('flip', { page: page});
        }

        var BaseListView = require('common/ListView');

        function EquipmentShopListView() {
            BaseListView.apply(this, arguments);
        }

        EquipmentShopListView.prototype.template = 'equipmentShopListPage';

        EquipmentShopListView.prototype.getUIProperties =function () {
            BaseListView.prototype.getUIProperties.apply(this, arguments);
            var str = location.href;
            var num = str.indexOf("=");
            if(num === -1){
                page = 1;
            }else {
                var name = str.substring(num-4, num);
                if(name === 'page'){
                    page = str.substring(num + 1, str.length);
                }else {
                    page = 1;
                }
            }
            showid = (page-1)*10+1;

            return {
                list: {
                    fields: fields,
                    sortable: false,
                    columnResizable: true,
                    subrow: false,
                    followHead: true,
                    selectMode: 'line',
                    noDataHtml: '<p style="margin:0;">暂无设备信息!</p>'
                }
            };
        }

        EquipmentShopListView.prototype.enterDocument = function () {
            BaseListView.prototype.enterDocument.apply(this, arguments);

            var me = this;
            this.get('list').on(
                'command',
                function (event) {
                    me.fire(event.name, event);
                }
            );

            this.get('createButton').on(
                'click',
                this.fire.bind(this, 'create')
            );
            //检索
            this.get('searchButton').on(
                'click',
                util.bind(search, this)
            );

            $('#list-page').on(
                'click',
                ':not(.disable)' ,
                util.bind(flip, this)
            );
        }

        require('er/util').inherits(EquipmentShopListView, BaseListView);
        return EquipmentShopListView;
    }
);
